package main

import (
	"fmt"
	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
)

type MysqlConfig struct {
	Host string `mapstructure:"host"`
	Port int    `mapstructure:"port"`
}
type ServerConfig struct {
	ServerName  string      `mapstructure:"ip_address"`
	MysqlConfig MysqlConfig `mapstructure:"mysql"`
}

func main() {
	v := viper.New()
	configFileName := "/Users/jinshijie/GolandProjects/shop_api/viper_tset/config-debug.yaml"

	v.SetConfigFile(configFileName)
	if err := v.ReadInConfig(); err != nil {
		panic(err)
	}
	serverConfig := ServerConfig{}
	err := v.Unmarshal(&serverConfig)
	if err != nil {
		panic(err)
	}
	fmt.Println(serverConfig)
	fmt.Println(serverConfig.MysqlConfig.Port)
	//动态监听变化
	v.WatchConfig()
	v.OnConfigChange(func(e fsnotify.Event) {
		if err := v.ReadInConfig(); err != nil {
			panic(err)
		}
	})

}
